package com.starryx.driverschool.service.impl;

import com.starryx.driverschool.dao.CarRepository;
import com.starryx.driverschool.po.Car;
import com.starryx.driverschool.po.Teacher;
import com.starryx.driverschool.service.CarService;
import com.starryx.driverschool.service.TeacherService;
import com.starryx.driverschool.utils.MyBeanUtils;
import com.starryx.driverschool.vo.CarQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CarServiceImpl implements CarService {

    @Autowired
    private CarRepository carRepository;

    @Autowired
    private TeacherService teacherService;

    @Override
    public Page<Car> listCar(Pageable pageable) {
        return carRepository.findAll(pageable);
    }

    @Override
    public List<Car> listCarWithoutTeacher() {
        return carRepository.findAllByTeacherNull();
    }

    @Override
    public Page<Car> listCar(Pageable pageable, CarQuery carQuery) {
        return carRepository.findAll(new Specification<Car>() {
            @Override
            public Predicate toPredicate(Root<Car> root,       // 相当于表名
                                         CriteriaQuery<?> cq,   // 条件查询执行器
                                         CriteriaBuilder cb) {  // 条件语句构建器，用于定义查询条件
                List<Predicate> predicates = new ArrayList<>();     // 查询条件组
                if (!"".equals(carQuery.getNo()) && carQuery.getNo() != null) {
                    predicates.add(cb.like(root.<String>get("no"), "%" + carQuery.getNo() + "%"));
                }
                if (!"".equals(carQuery.getType()) && carQuery.getType() != null) {
                    predicates.add(cb.like(root.<String>get("type"), "%" + carQuery.getType() + "%"));
                }
                cq.where(predicates.toArray(new Predicate[predicates.size()]));     // 将条件构造器构建的查询条件放到执行器执行
                return null;
            }
        }, pageable);
    }

    @Transactional
    @Override
    public Car saveCar(Car car) {
        return carRepository.save(car);
    }

    @Override
    public Car getCar(Long id) {
        return carRepository.findOne(id);
    }

    @Override
    public Car getCarByNo(String no) {
        return carRepository.findByNo(no);
    }

    @Transactional
    @Override
    public void deleteCar(Long id) {
        Teacher t = teacherService.getTeacherByCarId(id);
        if(t != null){
            t.setCar(null);
        }
        carRepository.delete(id);
    }
}
